package danny.hoi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import danny.hoi.dao.ArticleDao;
import danny.hoi.pojo.TbArticle;
import danny.hoi.service.ArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class ArticleServiceImpl implements ArticleService {

    //文章保存在redis中的key
    private static final String REDIS_ARTICLE = "REDIS_ARTICLE";

    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void add(TbArticle article) {
        article.setCreateTime(new Date());
        articleDao.insertSelective(article);
        deleteRedisArticle(article.getId());
    }

    @Override
    public void update(TbArticle article) {
        articleDao.updateByPrimaryKeySelective(article);
        deleteRedisArticle(article.getId());
    }

    @Override
    public void delete(Integer id) {
        articleDao.deleteByPrimaryKey(id);
        deleteRedisArticle(id);
    }

    @Override
    public TbArticle findOne(Integer id) {
        TbArticle article = null;
        try {
            //查询redis中是否用数据
          article = (TbArticle) redisTemplate.boundHashOps(REDIS_ARTICLE).get(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (article == null) {
            article = articleDao.selectByPrimaryKey(id);
            try {
                redisTemplate.boundHashOps(REDIS_ARTICLE).put(id, article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return article;
    }

    @Override
    public List<TbArticle> findAll() {
        return null;
    }


    @Override
    public PageInfo<TbArticle> search(Integer pageNum, Integer pageSize, TbArticle article) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(TbArticle.class);
        Example.Criteria criteria = example.createCriteria();
        //根据分类id查找
        if (article.getCategoryId() != null) {
            criteria.andEqualTo("categoryId", article.getCategoryId());
        }
        //根据关键字模糊查询
        if (StringUtils.isNotBlank(article.getTitle())) {
            criteria.andLike("title", "%" + article.getTitle() + "%");
            criteria.andLike("article", "%" + article.getTitle() + "%");
        }
        example.orderBy("createTime").desc();
        List<TbArticle> articleList = articleDao.selectByExample(example);

        return PageInfo.of(articleList);
    }

    //删除redis中的数据
    private void deleteRedisArticle(Integer id){
        try {
            redisTemplate.boundHashOps(REDIS_ARTICLE).delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
